package org.bluedream.core.module.service.warehouse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.map.HashedMap;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.comm.utils.StringUtils;
import org.bluedream.core.module.base.OrgBaseServiceImpl;
import org.bluedream.core.module.dao.warehouse.WareHouseDao;
import org.bluedream.core.module.entity.warehouse.WareHouse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WareHouseService
 * @Description TODO
 * @Author foxsand
 * @Data 2021-08-24 15:29
 * @Version
 */
@Service
@DS("master")
public class WareHouseService extends OrgBaseServiceImpl<WareHouseDao, WareHouse> implements IService<WareHouse> {
    private static final Logger logger = LoggerFactory.getLogger(WareHouseService.class);

    @Override
    public WareHouse getById(Serializable id) {
        if (EmptyUtil.isEmpty(id)){
            return new WareHouse();
        }
        return super.getById(id);
    }

    public IPage<WareHouse> findPage(WareHouse entity, String... args) {
        Map<String , Object> search = new HashedMap();
        if (EmptyUtil.isNoEmpty(entity.getCode())
                || EmptyUtil.isNoEmpty(entity.getName())
                || EmptyUtil.isNoEmpty(entity.getOrgCode())
        ){
            if (EmptyUtil.isNoEmpty(entity.getCode())){
                search.put("code" , entity.getCode());
            }
            if (EmptyUtil.isNoEmpty(entity.getName())){
                search.put("name" , entity.getName());
            }
            if (EmptyUtil.isNoEmpty(entity.getOrgCode())){
                search.put("ORG_CODE;eq" , entity.getOrgCode());
            }
        }
        return super.findPage(entity ,true , search , args);
    }

    /**
     * 获取树结构数据
     * @return
     */
    public List<Map<String , Object>> treeData(String excludeCode , String isShowCode){
        List<Map<String , Object>> mapList = new ArrayList<>();
        QueryWrapper<WareHouse> queryWrapper = new QueryWrapper<>(new WareHouse());
        queryWrapper.orderByAsc("code" );
        List<WareHouse> list = super.list(queryWrapper);
        for (WareHouse c1:list
        ) {
            if (!WareHouse.STATUS_NORMAL.equals(c1.getStatus())){
                continue;
            }
            if (EmptyUtil.isNoEmpty(excludeCode) && c1.getId().equals(excludeCode)){
                continue;
            }

            Map<String , Object> map = new HashMap<>();
            map.put("id" , c1.getId());
            map.put("pId" , "0");
            map.put("code" , c1.getCode());
            map.put("name" , StringUtils.getTreeNodeName(isShowCode , c1.getCode() , c1.getName()));
            map.put("isTreeLeaf" , true);
            map.put("isParent" , false);
            mapList.add(map);
        }
        return mapList;
    }
}
